package cn.hhchat.wp.dto.table;

import cn.hhchat.kit.annotation.ExcelColumn;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.apache.commons.lang3.StringUtils;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

/**
 * Created this one by HMH on 2017/7/24.
 * for element ui table
 */

@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
public class EleTable {


    List<EleHeader> header = new ArrayList<>();
    List body = new ArrayList<>();

    public EleTable(List list) {
        if (list.size() == 0) {
            return;
        }
        this.body = list;
        this.header = EleHeader.generateHeader(list.get(0).getClass());
    }



    @Getter
    @Setter
    @AllArgsConstructor
    @NoArgsConstructor
    static class EleHeader {
        String prop;
        String label;

        public static List<EleHeader> generateHeader(Class cls){
            List<EleHeader> headers = new ArrayList<>();
            Field[] fields = cls.getDeclaredFields();
            for (Field f : fields) {
                ExcelColumn column = f.getAnnotation(ExcelColumn.class);
                if (column == null) {
                    continue;
                }
                String value = column.value();
                if (StringUtils.isEmpty(value)) {
                    continue;
                }
                EleHeader header = new EleHeader(f.getName(), value);
                headers.add(header);
            }
            return headers;
        }
    }
}
